﻿--[[
   @brief    list control for contacts
   @author   perpu
   @version  1.0
   @date     2016-06-15
--]]
contact_list_control = 
{
    first_visible_item_index_ = 0;
    item_node_height_ = 58;
    is_inited_ = false;
    list_root_id_ = 0;
    timer_started_ = false;

    min_mode_root_id_ = 0;
    max_mode_root_id_ = 0;

    init = function()
        if contact_list_control.is_inited_ == false then
            base_utility.log("talk_dialog init");
            ierd_tgp.tpf_ui.Require("social\\contact_list\\model.lua");
            ierd_tgp.tpf_ui.Require("social\\contact_list\\contact_group_node.lua");
            ierd_tgp.tpf_ui.Require("social\\contact_list\\contact_message_list_model.lua");
            ierd_tgp.tpf_ui.Require("social\\contact_list\\contact_group_list_control.lua");
            ierd_tgp.tpf_ui.Require("social\\contact_list\\contact_discuss_list_control.lua");
            contact_list_control.is_inited_ = true;
        end
    end;

    
    on_create = function (uilib, ctrl_id)
        local out = comm_center.call_service("i_service_mainframe_get_current_mode", {});
        if out.mode == 0 then --max
            contact_list_control.max_mode_root_id_ = ctrl_id;
            base_utility.log("[contact_list_control] max mode ctrl_id:"..tostring(ctrl_id));
        else
            contact_list_control.min_mode_root_id_ = ctrl_id;
            base_utility.log("[contact_list_control] mini mode ctrl_id:"..tostring(ctrl_id));
        end

        contact_list_control.list_root_id_ = ctrl_id;

        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddInterval('contact_list_control.on_init', 1);
    end;

    on_init = function ()
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval('contact_list_control.on_init');
        contact_list_control.first_visible_item_index_ = 0;
        contact_list_control.set_total_height(contact_list_model.get_total_item_height());

        local prepared_visible_count = 20;--可视窗口的item数量
        contact_list_control.set_visible_window_height(prepared_visible_count * contact_list_control.item_node_height_);
        
        contact_list_model.virtual_node_id_to_group_info = {};

        if contact_list_control.item_list_ ~= nil then
            for key,value in pairs(contact_list_control.item_list_) do
                -- 删除所有item数据。
                local id = value['frame_id'];
                if id~= nil then
                    layout_mgr.remove_ctrl(id);
                end;
            end;
        end;

        contact_list_control.item_list_ = {};

        if #contact_list_control.item_list_ == 0 then
            contact_list_control.item_list_ = {};
            for i = 1, prepared_visible_count do
                table.insert(contact_list_control.item_list_, {});
            end;
        end;
        contact_list_control.update_ui();
        contact_list_control.start_update_ui_timer();
    end;

    start_update_ui_timer = function()
        if contact_list_model.get_group_list_done == false then
            base_utility.log("[contact_list_control] start_update_ui_timer get_group_list_done false!", true);
            return;
        end
        if contact_list_control.timer_started_ == false then
            contact_list_control.timer_started_ = true;
            local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            tpfui:AddInterval('contact_list_control.on_timer', 1000);
            base_utility.log("[contact_list_control] start_update_ui_timer success!", true);
        end
    end;

    stop_update_ui_timer = function()
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval('contact_list_control.on_timer');
    end;

    on_timer = function()
        if group_list_model.data_refreshed_ == nil then
        group_list_model.data_refreshed_ = {};
        end;
        if friend_list_model.data_refreshed_ == nil then
            friend_list_model.data_refreshed_ = {};
        end;

        if group_list_model.data_refreshed_['contact_list'] == nil or friend_list_model.data_refreshed_['contact_list'] == nil then
            group_list_model.data_refreshed_['contact_list'] = true;
            friend_list_model.data_refreshed_['contact_list'] = true;
        else
            return;
        end;
        base_utility.log("[contact_list_control]auto_update_vlist");
        contact_list_control.update_ui();
    end;
    
    set_visible_window_height = function(total_height)
        ui_utility.setproperty_by_id(contact_list_control.list_root_id_,'Height', total_height);
    end;

    set_total_height = function(total_height)
        local id_back_panel = ui_utility.get_ctrl_parent_id(contact_list_control.list_root_id_);
        ui_utility.setproperty_by_id(id_back_panel,'Height', total_height);
    end;

    b_doing = false;
    time_of_last_doing = 0;

    on_test_scroll = function (a,b,c,d,e,args)
        -- local id_stack_panel = ui_utility.get_ctrl_id_by_cid(contact_list_control.list_root_id_);
        local id_scrollview = ui_utility.get_ctrl_parent_id(ui_utility.get_ctrl_parent_id(contact_list_control.list_root_id_));
        local scroll_view = ui_utility.get_ctrl_ptr_by_id(id_scrollview);        
        local vbar = scroll_view:GetVScrollBar();
        contact_list_control.last_step_index_ = vbar:GetScrollPos();
        local offset = contact_list_control.last_step_index_ * 20;

        contact_list_control.first_visible_item_index_ = contact_list_model.first_visible_item_index(offset);
        local first_item_offset = contact_list_model.offset_of_first_visible_item_index(contact_list_control.first_visible_item_index_);

        --ui_utility.setproperty_by_id(contact_list_control.list_root_id_,'YOffset', first_item_offset);--todu
        local ptr_stack = ui_utility.get_ctrl_ptr_by_id(contact_list_control.list_root_id_);
        local rect = ptr_stack:GetRectEx(FALSE,TRUE);
        local height = rect.bottom - rect.top;
        rect.top = first_item_offset;
        rect.bottom = rect.top + height;
        ptr_stack:MoveEx(rect,TRUE);

        if contact_list_control.b_doing == false and os.clock() - contact_list_control.time_of_last_doing > 1 then
            contact_list_control.b_doing = true;
            contact_list_control.update_ui();
            contact_list_control.b_doing = false;
            contact_list_control.time_of_last_doing = os.clock();
        else
            local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            tpfui:AddInterval('contact_list_control.do_scrolled_data', 0);
        end;
        --contact_list_control.update_ui();
    end;
    
    item_list_ = {};
    
    do_scrolled_data = function()
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval('contact_list_control.do_scrolled_data');
        contact_list_control.update_ui();
        contact_list_control.b_doing = false;
        contact_list_control.time_of_last_doing = os.clock();
    end;

    update_ui = function (args)
        contact_list_control.set_total_height(contact_list_model.get_total_item_height());
        
        local first_visible_item_index = contact_list_control.first_visible_item_index_;

        local id_stack_panel = contact_list_control.list_root_id_;
        
        local dynamic_stack_height = 0;
        for i = 1, #contact_list_control.item_list_ do
            --local p_item = ui_utility.get_ctrl_ptr_by_path(id_stack_panel, "item_" .. tostring(i));
            --local str, str_holder = U8toCUIStr("item_" .. tostring(i+first_visible_item_index));
            --p_item:SetCaption(str);
            local nodetype, ui, info, item_height = contact_list_model.get_item_info(i-1+contact_list_control.first_visible_item_index_);

            if nodetype == nil then
                if contact_list_control.item_list_[i]['frame_id'] then --隐藏节点
                    ui_utility.setproperty_by_id(contact_list_control.item_list_[i]['frame_id'],'Visible', 'false');
                end;
            else
                if contact_list_control.item_list_[i]['frame_id'] then --显示节点
                    ui_utility.setproperty_by_id(contact_list_control.item_list_[i]['frame_id'],'Visible', 'true');
                end;

                local ui_str, str_holder = U8toCUIStr(ui);
            
                --create node
                --contact_list_control.item_list_[i]['xml'] = ui;
            
                if contact_list_control.item_list_[i]['frame_id'] == nil then
                    nodeframe_id = layout_mgr.restore_sub_layout_multi(id_stack_panel, "social\\v_list_node.tpf");
                    contact_list_control.item_list_[i]['frame_id'] = nodeframe_id;
                end;

                if contact_list_control.item_list_[i]['vec'] == nil then
                    contact_list_control.item_list_[i]['vec'] = {}
                end;

                if contact_list_control.item_list_[i]['vec'][nodetype] == nil then
                    contact_list_control.item_list_[i]['vec'][nodetype] = {}
                end;

                for key, value in pairs(contact_list_control.item_list_[i]['vec']) do  
                    if key == nodetype then
                        if value['id'] ~= nil then
                            ui_utility.setproperty_by_id(value['id'],'Visible', 'true');
                        end;
                    else
                        if value['id'] ~= nil then
                            ui_utility.setproperty_by_id(value['id'],'Visible', 'false');
                        end;
                    end;
                end;


                if contact_list_control.item_list_[i]['vec'][nodetype]['id'] == nil then
                    control_id = layout_mgr.restore_sub_layout_multi(contact_list_control.item_list_[i]['frame_id'], ui);
                    contact_list_control.item_list_[i]['vec'][nodetype]['id'] = control_id;
                end;

                ui_utility.setproperty_by_id(contact_list_control.item_list_[i]['frame_id'], 'Height', item_height);
                contact_group_node.update(nodetype, contact_list_control.item_list_[i]['vec'][nodetype]['id'], info);

                dynamic_stack_height = dynamic_stack_height + item_height;
            end;
        end
        ui_utility.setproperty_by_id(id_stack_panel, 'Height', dynamic_stack_height);
        
    end;

    clicked_treenode = function()
        contact_list_model.clicked_treenode();
        contact_list_control.set_total_height(contact_list_model.get_total_item_height());
        contact_list_control.update_ui();
    end;
    clicked_grouptreenode = function()
        contact_list_model.clicked_grouptreenode();
        contact_list_control.set_total_height(contact_list_model.get_total_item_height());
        contact_list_control.update_ui();
    end;

    last_step_index_ = 0;
    fix_scrollbar_on_size_change = function()
        local id_scrollview = ui_utility.get_ctrl_parent_id(ui_utility.get_ctrl_parent_id(contact_list_control.list_root_id_));
        local scroll_view = ui_utility.get_ctrl_ptr_by_id(id_scrollview);
        if scroll_view ~= nil then
            local p_rc = scroll_view:GetRectEx(FALSE,TRUE);
            local new_visible_height = p_rc.bottom - p_rc.top;
            local vbar = scroll_view:GetVScrollBar();
            if vbar ~= nil then
                contact_list_control.set_total_height(contact_list_model.get_total_item_height());
                vbar:SetScrollInfo(contact_list_model.get_total_item_height(), new_visible_height, 20, contact_list_control.last_step_index_*20, TRUE);
            end;
        end;
    end;

    get_recomment_group_success = function()
        contact_list_control.update_ui();
    end;
};

contact_list_control.init();